﻿using DG.Tweening;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    public class UIComponentBase : ComponentBase, IDestroy
    {
        public GameObject gameObject { get; private set; }
        public Transform transform { get; private set; }
        public RectTransform rectTransform { get; private set; }
        public UILayer layer { get; protected set; }
        public bool isFloat { get; protected set; }

        private bool _isVisible;
        public bool IsVisible { get { return _isVisible; } }

        protected async CTask<bool> SpawnPrefab(string prefabPath, UILayer layer, bool isFloat)
        {
            this.layer = layer;
            this.isFloat = isFloat;
            GameObject prefab = await ResManager.Instance.LoadAsset<GameObject>(prefabPath);
            if (!prefab)
            {
                LogError($"加载预置体失败（是否AB属于延迟加载部分？否则Shift+A,勾选自动更新）[{prefabPath}]");
                return false;
            }
            Transform parent = null;
            switch (layer)
            {
                case UILayer.Background:
                    parent = UILayerControl.instance.backgroundRoot;
                    break;
                case UILayer.Normal:
                    parent = UILayerControl.instance.normalRoot;
                    break;
                case UILayer.Tip:
                    parent = UILayerControl.instance.tipRoot;
                    break;
                case UILayer.Top:
                    parent = UILayerControl.instance.topRoot;
                    break;
            }
            gameObject = GameObject.Instantiate(prefab, parent, false);
            transform = gameObject.transform;
            if (transform is RectTransform) rectTransform = transform as RectTransform;
            AddComponent<UISortingComponent, GameObject>(gameObject);
            UIManagerComponent.instance.OnUICreate(this, layer);
            gameObject.AddEnableEvent(OnEnable);
            gameObject.AddDisableEvent(OnDisable);
            return true;
        }

        public async void ScaleIn(Transform dialog, float bgAlpha = 0.85f, float duration = 0.25f, float beginScale = 0.7f)
        {
            Image black = dialog.parent.GetComponent<Image>();
            if (black) black.color = new Color(0, 0, 0, 0);
            dialog.localScale = Vector3.zero;
            /*ScrollRect[] scrollRects = dialog.GetComponentsInChildren<ScrollRect>();
            bool[] defaultEnable = new bool[scrollRects.Length];
            for (int i = 0; i < scrollRects.Length; i++)
            {
                defaultEnable[i] = scrollRects[i].enabled;
                scrollRects[i].enabled = false;
            }*/

            await CTask.Wait(0.2f);

            if (black) black.DOColor(new Color(0, 0, 0, bgAlpha), duration);
            dialog.localScale = Vector3.one * beginScale;
            dialog.DOScale(Vector3.one * 1.03f, duration * 0.7f).SetEase(Ease.OutSine).OnComplete(() =>
            {
                dialog.DOScale(Vector3.one, duration * 0.3f).SetEase(Ease.OutSine).OnComplete(() =>
                {
                    /*for (int i = 0; i < scrollRects.Length; i++)
                    {
                        scrollRects[i].enabled = defaultEnable[i];
                    }*/
                });
            });
        }

        public virtual void BlackboardFadeIn(float duration = 0.3f)
        {
            transform.DOAlpha(0, false);
            transform.DOAlpha(0.75f, duration, false);
        }

        public virtual async void RiseIn(RectTransform dialog, float duration = 0.3f, float toY = 0f, float blackAlpha = 0.5f)
        {
            Image black = dialog.parent.GetComponent<Image>();
            if (black) black.color = new Color(0, 0, 0, 0);
            dialog.DOAlpha(0);
            dialog.anchoredPosition = new Vector2(0, toY - ScreenHelper.screenUISize.y * 0.1f);

            await CTask.Wait(0.1f);

            if (black) black.DOColor(new Color(0, 0, 0, blackAlpha), duration);
            dialog.DOAlpha(1, duration * 0.5f).SetEase(Ease.Linear);
            dialog.DOAnchorPosY(toY, duration).SetEase(Ease.OutBack);
        }

        public void _SetUIVisible(bool b)
        {
            //IU层级控制专用
            _isVisible = b;
        }

        public virtual void OnEnable() { }
        public virtual void OnDisable() { }

        public virtual void OnDestroy()
        {
            this._isVisible = false;
            if (gameObject)
            {
                gameObject.RemoveEnableEvent(OnEnable);
                gameObject.RemoveEnableEvent(OnDisable);
                GameObject.Destroy(gameObject);
            }
        }
    }
}
